<?php

namespace App\Events;
use App\Model\Business\Schedule;
use App\Model\Eeoapi\Eeocourse;
use App\Model\Eeoapi\Eeoschedule;
use App\Model\Foundation\Student;
use App\Model\System\Eeofile;

class AppointEvent extends Event
{
    ####
    # 本地类型
    public $type ; // 班级类型
    public $chimpCourseId ; //Chimp 课程ID

    #### eeo 需要数据
    public  $folderId; //eeo 云盘目录ID
    public  $Filedata; //eeo 课程封面
    public  $courseId; //eeo 课程ID
    public  $courseName; //eeo 课程名称
    public  $expiryTime; //eeo 课程过期时间

    /**
     * 学生数据
     * @var array
     * @refer setStudent
     */
    public   $identity;
    private  $students;
    private  $oldStudents;


    /**
     * 课节数据
     * @var array
     * @refer setLesson
     */
    private  $lessons;


    /**
     * CourseEvent constructor.
     * @param $action
     * @param $schedule
     */
    public function __construct(Schedule $schedule, $action=null)
    {
        $this->lessons = [];
        $this->students = [];
        $this->oldStudents = [];
        $photo = Event::getOrg('defaultCourseImage');
        if(!empty($photo)){
            $photo = str_replace('/api/file/', '', $photo);
            $photo = str_replace('/api/file', '', $photo);
            $photo = str_replace('?u=', '', $photo);
            $photo = Eeofile::getLocalPath($photo);
            if(file_exists($photo))
                $this->Filedata = $photo;
        }

        $this->action = $action;
        $this->type = Schedule::APPOINT;

        $this->chimpCourseId = $schedule->scheduleid;
        $this->courseName = $schedule->name;

        if(!is_null($schedule->folderid)){
            $this->folderId = $schedule->folderid;
        }
        # 回查
        if($action <> Event::CREATE){
            $eeocourse = Eeocourse::where('type', $this->type)
                        ->where('chimpid', $this->chimpCourseId)
                        ->first();
            if($eeocourse){
                $this->courseId = $eeocourse->eeocourseid;
            }
        }
    }

    /**
     * 设置EEO 返回的课程id
     * @param $id
     */
    public function setCourseID($id)
    {
        $this->courseId = $id;

        $fileds = [
            'type'=>$this->type,
            'chimpid'=>$this->chimpCourseId,
            'eeocourseid'=>$this->courseId
        ];
        Eeocourse::firstOrCreate($fileds);

    }

    public function delCourseID()
    {
        Eeocourse::where('chimpid',$this->chimpCourseId)->delete();
        Eeoschedule::where('scheduleid',$this->chimpCourseId)->delete();
    }

    public function setIdentity($code=1)
    {
        $this->identity = $code;
    }

    public function getIdentity()
    {
        return $this->identity;
    }

    /**
     * 返回学生信息
     * @return array
     */
    public function getStudents($old=false)
    {
        if($old){
            return $this->oldStudents;
        }
        return $this->students;
    }

    /**
     * 附加学生
     * @param $student
     */
    public function setStudent($student, $old=false)
    {
        if($old){
            $id = $student->studentuid;
            if(!key_exists($id, $this->students)){
                $this->oldStudents[$id]['identity'] = $this->getIdentity();
                $this->oldStudents[$id]['studentName'] = $student->name;
                $this->oldStudents[$id]['studentAccount'] = $student->ClassInNo;
            }
        }else{
            $id = $student->studentuid;
            if(!key_exists($id, $this->students)){
                $this->students[$id]['identity'] = $this->getIdentity();
                $this->students[$id]['studentName'] = $student->name;
                $this->students[$id]['studentAccount'] = $student->ClassInNo;
            }
        }
    }

    /**
     * 批量添加学生
     * @param Student $students
     */
    public function setStudents($students, $old=false)
    {
        foreach($students as $student){
            $this->setStudent($student, $old);
        }
    }

    /**
     * 清除学生
     */
    public function unsetStudents()
    {
        $this->students = [];
    }

    /**
     * 获取课程
     * @return mixed
     */
    public function getLessons()
    {
        return $this->lessons;
    }

    /**
     * 添加一个课程
     * @param Schedule $schedule
     */
    public function setLesson(Schedule $schedule, $action)
    {
        $this->lessons[] = new ScheduleEvent($schedule, $action);
    }

    /**
     *  批量添加课程
     * @param Schedule $schedules
     */
    public function setLessons(Schedule $schedules, $action)
    {
        foreach($schedules as $schedule){
            $this->setLesson($schedule, $action);
        }
    }

    /**
     * 记录
     */
    public function __destruct()
    {

    }


}
